﻿using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Infrastructure;
using System.Collections.Generic;
using Bordspil.Models;
using System.Linq;
using System;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Bordspil.Controllers
{
    public class LudoHub : Hub
    {
        public string moveFrom { get; set; }
        public string goHere { get; set; }

        IAppRepository m_db = new AppRepository();

        public void Join(string groupId, string userName)
        {
            int id;
            id = Int32.Parse(groupId); //henti groupId i int gildi, tvi instanceId er int i database

            PlayerList p = new PlayerList();
            p.PlayerName = userName;
            p.id = id;
            p.IsItMyTurn = false;
            m_db.AddToPlayerList(p);
            m_db.Save();

            Groups.Add(Context.ConnectionId, groupId);

        }

        public void StartGame(string groupName)
        {
            var id2 = Convert.ToInt32(groupName);
            var result = (from r in m_db.GetPlayerList()
                          where r.id == id2
                          select r);
            var counter = result.Count();
            
            //ef naegur notandafjoldi er komin i leikinn ta ma byrja leik
            if (result.Count() == 4) 
            {
                Clients.Group(groupName).startGame("Byrjid Leik");

            }
            else
            {
                Clients.Group(groupName).startGame("Bid eftir motspilara");
            }
        }



        public void WhoStarts(string groupName)
        {
            //segir til um hver byrjar, sem er alltaf sa adili sem byr til leik, first instance.userName i GameInstance 
            var id2 = Convert.ToInt32(groupName);
            var result = (from w in m_db.GetPlayerList()
                          where w.id == id2
                          select w).FirstOrDefault();
            if (result != null)
            {
                Clients.Group(groupName).whoStarts(result.PlayerName);
            }
            else
            {
                Clients.Group(groupName).whoStarts("Engin byrjar");
            }

        }

        public void WhosNext(string groupName, string userName)
        {
            if (userName != null)
            {
                Clients.Group(groupName).whosNext(userName);
                
            }
            else
            {
                Clients.Group(groupName).whosNext("Engin ma gera naest");
            }
        }

        public void ConnectedPlayers(string groupName)
        {
            //skilar ollum notendum sem eru tengdir sama instanceid
            var id2 = Convert.ToInt32(groupName);
            var result = from c in m_db.GetPlayerList()
                         where c.id == id2
                         select c;

            Clients.Group(groupName).connectedPlayers(result);
        }


        public void DrawMyMove(string groupName, string moveFrom, int thisFar)
        {
            if (thisFar > 6 || thisFar < 0)
            {
                //passa ad notandi se ekki ad gera eitthvad ologlegt
                return;
            }

            Clients.Group(groupName).drawMyMove(moveFrom, thisFar);
        }
    }
}